﻿using MaterialDesignThemes.Wpf;
using MyFirstWPF.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyFirstWPF.View.Compnent
{
    /// <summary>
    /// AddDiary.xaml 的交互逻辑
    /// </summary>
    public partial class AddDiary : UserControl
    {
        private const String DEFAULT_TEXT = "标题";
        TaskInfo MyTask;
        Window2 MyWin2;
        public AddDiary(TaskInfo task)
        {
            InitializeComponent();
            MyTask = task;
            MyWin2 = LoginView.window2Instance;
            HintAssist.SetHint(title, "任务[" + task.Name + "]已经完成，快点开始写日志吧");
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            if (title.Text == DEFAULT_TEXT)
            {
                title.Text = "无标题";
            }
            if (InputJournal.Text == "")
            {
                StackPanel st = new StackPanel();
                st.Margin = new Thickness(16);
                TextBlock tb = new TextBlock();
                tb.FontSize = 20;
                tb.Text = "日志内容不能为空";
                st.Children.Add(tb);
                Button bt = new Button();
                bt.Content = "ok";
                bt.Click += (oo, ee) => {
                    DialogHost.CloseDialogCommand.Execute(null, null);
                };
                st.Children.Add(bt);
                DialogHost.OpenDialogCommand.Execute(st, null);
                return;
            }
            else {              
                using (var db = new StudyPlanDb(Page1.dbPath))
                {
                    MyTask.isFinished = 1;
                    db.Update(MyTask);
                    DiaryInfo diary = new DiaryInfo();
                    diary.DiaryTitle = title.Text;
                    diary.Diary = InputJournal.Text;
                    diary.BuildTime = DateTime.Now.ToString();
                    diary.userId = LoginView.myId;
                    diary.TaskId = MyTask.Id;
                    db.Insert(diary);
                }
                DialogHost.CloseDialogCommand.Execute(null, null);
                MyWin2.frame1.Refresh();
                MyWin2.frame2.Refresh();
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {           
            DialogHost.CloseDialogCommand.Execute(null, null);
        }

        private void Title_DragEnter(object sender, DragEventArgs e)
        {
        }
    }
}
